﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Citi.RoutineBase;
using Citi.ATM.Domain;
using Citi.Helpers;
using Citi.ATM.SchedulerUI.EmailSenderWcf;

namespace Citi.ATM.SchedulerUI
{
    public class DailyReport : IExecutableRoutine
    {
        public void Rollback(ref ICurrentAction currentAction)
        {

        }

        public void Start(DateTime scheduledDate, ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            currentAction.Description = "Sending mails";
            SendEmail(GetDailyHtml(parameters), string.Format("***Eventos Criticos de ATM - Relatório Diário {0}***", DateTime.Today.ToString("dd/MM/yyyy")), "*CTI BR ATM Services Reports <dl.cti.br.atm.services.reports@imcla.lac.nsroot.net>", "");
            //SendEmail(GetDailyHtml(parameters), string.Format("***Eventos Criticos de ATM - Relatório Diário {0}***", DateTime.Today.ToString("dd/MM/yyyy")), "fc16291", "");
        }


        public void SendEmail(string template, string subject, string to, string cc)
        {
            using (var client = new EmailClient())
            {
                client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

                var email = new EmailSenderWcf.Email
                {
                    BodyMessage = template,
                    Subject = subject,
                    SentOnBehalfOfName = "O&T Finance Comunica",
                    To = to.Split(';').Select(x => new EmailSenderWcf.EmailAddress { Description = x }).ToList(),
                    Cc = cc.Split(';').Select(x => new EmailSenderWcf.EmailAddress { Description = x }).ToList(),
                    ApplicationName = "ATM Automation",
                    Url = string.Empty,
                    Parameters = new Dictionary<string, string>()
                };

                client.InsertEmailInQueue(email);
            }
        }

        public void Stop(ref ICurrentAction currentAction)
        {

        }

        public string GetDailyHtml(Dictionary<string, object> parameters)
        {
            var events = new ATMEvents().GetByStatus("Aberto");

            var template = EmailHelper.GetEmailBody(@"\\LACBRA001FS0010\DATA_GRP1\FTOOLS\Apps\ATMAutomation\HTML\DailyEmail.htm");
            var templateContent = string.Empty;
            var filledTemplateContent = string.Empty;

            if (events.Any())
            {
                templateContent = EmailHelper.GetEmailBody(@"\\LACBRA001FS0010\DATA_GRP1\FTOOLS\Apps\ATMAutomation\HTML\DailyContent.htm", 
                    parameters["RobotUsername"].ToString(), parameters["RobotPassword"].ToString());

                foreach (var item in events)
                {
                    var templateFill = templateContent;

                    templateFill = templateFill.Replace("[Branch]", string.Concat(item.TipoValue, " ", item.Nome));
                    templateFill = templateFill.Replace("[equipment]", item.Identificação);
                    templateFill = templateFill.Replace("[incident]", item.Incident);
                    templateFill = templateFill.Replace("[description]", item.Motivo);
                    templateFill = templateFill.Replace("[descriptiondetails]", item.Incidente);
                    templateFill = templateFill.Replace("[yes/not]", item.BackupValue);
                    templateFill = templateFill.Replace("[howmany]", item.Quantos);
                    templateFill = templateFill.Replace("[dateandhour]", item.Data.ToString());
                    templateFill = templateFill.Replace("[unavailability]", item.IndisponibilidadeMin);
                    templateFill = templateFill.Replace("[resposible]", item.SuporteTécnico);

                    filledTemplateContent += templateFill;
                }
            }

            else
            {
                templateContent = EmailHelper.GetEmailBody(@"\\LACBRA001FS0010\DATA_GRP1\FTOOLS\Apps\ATMAutomation\HTML\DailyNullContent.htm");
                filledTemplateContent += templateContent;
            }

            return template.Replace("[DailyContent]", filledTemplateContent);
        }
    }
}
